package com.etc.web;



import com.etc.model.Leave_paper;
import com.etc.model.Publictell;
import com.etc.model.Teacher;
import com.etc.model.User_class;
import com.etc.service.impl.TeacherServiceImpl;
import com.etc.vo.LeavePaperDateToStringVo;
import com.etc.vo.PublictellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 主页页面控制器
 */
@Controller
@RequestMapping(value = "/Teacher")
public class TeacherControler {
    @Autowired
    private TeacherServiceImpl teacherService;
    //防止出错
    private List<String> attendance ;
    private List<String> user;
    //教师注册
    @RequestMapping(value = "/register")
    public String RegisterTeacher( Teacher teacher) {
        teacherService.RegisterTeacher(teacher);
        return "redirect:/index";
    }

    //注册账号重复检测
    @RequestMapping(value = "/login/TeacherRegisterExam" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String, String> loginRegisterExam(@RequestBody Teacher data){
        Map<String,String> map=new HashMap<String, String>();
        map = teacherService.loginRegisterExam(data);
        return map;
    }

    //登录检测
    @RequestMapping(value = "/login/TeacherExam" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String, String> loginExam(@RequestBody Teacher teacher,HttpSession httpSession){
        httpSession.setAttribute("username",teacher.getUsername());
        Map<String,String> map=new HashMap<String, String>();
        map = teacherService.loginExam(teacher);
        return map;
    }

    //教师登录界面
    @RequestMapping(value = "/login/TeacherExam/loginUser")
    public String TeacherLoginUser(HttpSession httpSession) {
        ModelAndView view = new ModelAndView();
        view.addObject("username",httpSession.getAttribute("username"));
        //存储相关信息
        Teacher teacher = teacherService.TeacherLoginUser((String)httpSession.getAttribute("username"));
        httpSession.setAttribute("name",teacher.getName());
        httpSession.setAttribute("school",teacher.getSchool());
        httpSession.setAttribute("id",teacher.getId());
        return "redirect:/Teacher/login/ClassNext";
    }

    //刷新列表
    @RequestMapping(value = "/login/ClassNext")
    public ModelAndView TeacherClassNext(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //自动查找相应的班级
        List<User_class> list = teacherService.TeacherClassNext((String)httpSession.getAttribute("username"));
        view.addObject("classList",list);
        view.addObject("username",httpSession.getAttribute("username"));
        view.setViewName("/indexTeacher" );
        return view;
    }

    //创建班级
    @RequestMapping(value = "/login/TeacherExam/loginUser/createClass")
    public String TeacherCreateClass(String btn_classN,HttpSession httpSession){
        teacherService.TeacherCreateClass((String)httpSession.getAttribute("username"),(String)httpSession.getAttribute("name"),btn_classN);
        return "redirect:/Teacher/login/ClassNext";
    }

    //解散课堂
    @RequestMapping(value = "/login/TeacherExam/loginUser/breakClass")
    public String TeacherBreakClass(String classnode){
        teacherService.TeacherBreakClass(classnode);
        return "redirect:/Teacher/login/ClassNext";
    }

    //离开课堂
    @RequestMapping(value = "/login/TeacherExam/loginUser/exitClass")
    public String TeacherExitClass(String classnode,HttpSession httpSession){
        teacherService.TeacherExitClass((String)httpSession.getAttribute("username"),classnode);
        return "redirect:/Teacher/login/ClassNext";
    }

    //修改课堂
    @RequestMapping(value = "/login/TeacherExam/loginUser/modifyClass")
    public String TeacherModifyClass(User_class user_class,HttpSession httpSession){
        teacherService.TeacherModifyClass(user_class,(String)httpSession.getAttribute("username"));
        return "redirect:/Teacher/login/ClassNext";
    }

    //加入班级
    @RequestMapping(value = "/login/TeacherExam/loginUser/enterClass", method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String,String> TeacherEnterClass(String classnode,HttpSession httpSession){
        Map<String,String> map = new HashMap<>();
       map = teacherService.TeacherEnterClass(classnode,(String)httpSession.getAttribute("username")
                ,(String)httpSession.getAttribute("name"));
        return map;
    }

    //进入班级考勤页面
    @RequestMapping(value = "/login/TeacherExam/loginUser/attendance")
    public ModelAndView jumpAttendance(String classnode,HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //记录班级邀请码
        httpSession.setAttribute("classnode",classnode);
        //获取自己的身份
        String usertype = teacherService.jumpAttendanceUsertype((String)httpSession.getAttribute("username"),(String)httpSession.getAttribute("classnode"));
        //绑定自己的usertype
        httpSession.setAttribute("usertype",usertype);
        view.addObject("usertype",usertype);
        //记录班级名
        User_class user_class = teacherService.jumpAttendanceClassName(classnode);
        view.addObject("classname",user_class.getClassname());
        httpSession.setAttribute("classname",user_class.getClassname());
        //用户相关信息
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",classnode);
        view.addObject("page","attendance");

        //考勤数据获取
        //获取班级内学生全部人数
        List<User_class> list = teacherService.jumpAttendanceList(classnode);
        //获取班级内所有考勤状态
        String all_data[][] = teacherService.jumpAttendanceAll_data(list,(String)httpSession.getAttribute("classnode"));
        //获取班级内所有班级内的名字
        user = teacherService.jumpAttendanceUser(list);
        //获取班级内所有班级内的学号
        String[] stuNumber = teacherService.jumpAttendanceStuNum(list);
        //获取班级内所有班级内的考勤日期
        attendance =  teacherService.jumpAttendanceAttendance(list,(String)httpSession.getAttribute("classnode"));

        view.setViewName("/content");
        view.addObject("user",user);
        view.addObject("stuNumber",stuNumber);
        view.addObject("all_data",all_data);
        view.addObject("attendance",attendance);

        //整合所有数据，以便输出excel
        List<Map<String,String>> exportExcelList = teacherService.jumpAttendanceExportExcelList(user,attendance,all_data);
        httpSession.setAttribute("exportExcelList",exportExcelList);
        return view ;
    }

    //新建班级考勤日期
    @RequestMapping(value = "/login/TeacherExam/loginUser/createAttendance")
    public String createAttendance(HttpSession httpSession){
       teacherService.createAttendance((String)httpSession.getAttribute("classnode"));
        return "redirect:/Teacher/login/TeacherExam/loginUser/attendance?classnode="+httpSession.getAttribute("classnode");
    }

    //修改班级考勤日期
    @RequestMapping(value = "/login/TeacherExam/loginUser/ModifyAttendance")
    public String ModifyAttendance(String attendance_Old ,String attendance_New ,HttpSession httpSession){
        teacherService.ModifyAttendance(attendance_Old,attendance_New,(String)httpSession.getAttribute("classnode"));
        return "redirect:/Teacher/login/TeacherExam/loginUser/attendance?classnode="+httpSession.getAttribute("classnode");
    }

    //修改班级考勤状态
    @RequestMapping(value = "/login/TeacherExam/loginUser/ModifyAttendanceStatus")
    public String ModifyAttendanceStatus(String type ,String number ,String user,HttpSession httpSession){
        teacherService.ModifyAttendanceStatus(type,number,user,(String)httpSession.getAttribute("classnode"));
        return "redirect:/Teacher/login/TeacherExam/loginUser/attendance?classnode="+httpSession.getAttribute("classnode");
    }

    //删除考勤信息
    @RequestMapping(value = "/login/TeacherExam/loginUser/DeleteAttendance")
    public String DeleteAttendance(String attendance ,HttpSession httpSession){
        teacherService.DeleteAttendance(attendance,(String)httpSession.getAttribute("classnode"));
        return "redirect:/Teacher/login/TeacherExam/loginUser/attendance?classnode="+httpSession.getAttribute("classnode");
    }

    //成员页面
    @RequestMapping(value = "/login/TeacherExam/loginUser/jump/member")
    public ModelAndView jumpmember(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //用户相关信息
        view.addObject("page","member");
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",httpSession.getAttribute("classnode"));
        view.addObject("classname",httpSession.getAttribute("classname"));
        //班级所有信息
        List<User_class> allClass = teacherService.jumpmember((String)httpSession.getAttribute("classnode"));
        view.addObject("studentlist",allClass);
        view.addObject("attendance",attendance);
        view.addObject("user",user);
        view.addObject("usertype",httpSession.getAttribute("usertype"));
        view.setViewName("/content");
        return view ;
    }

    //成员页面-删除成员
    @RequestMapping(value = "/deleteStuInfo")
    public String DeleteStudentInfo( int id) {
        teacherService.DeleteStudentInfo(id);
        return "redirect:/Teacher/login/TeacherExam/loginUser/jump/member";
    }

    //通知页面
    @RequestMapping(value = "/login/TeacherExam/loginUser/jump/publictell")
    public ModelAndView jumppublictell(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //用户相关信息
        view.addObject("page","publictellPage");
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",httpSession.getAttribute("classnode"));
        view.addObject("classname",httpSession.getAttribute("classname"));
        view.addObject("attendance",attendance);
        view.addObject("user",user);

        List<Publictell> publictells=teacherService.jumppublictell((String)httpSession.getAttribute("classnode"));
        PublictellVo publictellVo = new PublictellVo();
        publictellVo.setPublictell(publictells);
        view.addObject("publictellVos",publictellVo);
        //功能信息
        view.setViewName("/content");
        view.addObject("usertype",httpSession.getAttribute("usertype"));
        return view ;
    }

    //插入公告具体信息
    @RequestMapping(value = "/submitPublictell")
    @ResponseBody
    public PublictellVo submitPublictell(@RequestBody Publictell publictell, HttpSession httpSession) {
        PublictellVo publictellVos = teacherService.submitPublictell(publictell,(String) httpSession.getAttribute("username")
                ,(String) httpSession.getAttribute("classnode"));
        return publictellVos;
    }

    //通知页面删除
    @RequestMapping(value = "/deleteStuPublictell")
    public String DeleteStudentPublictell( int id) {
        teacherService.DeleteStudentPublictell(id);
        return "redirect:/Teacher/login/TeacherExam/loginUser/jump/publictell";
    }

    //特殊情况页面
    @RequestMapping(value = "/login/TeacherExam/loginUser/jump/specialcase")
    public ModelAndView jumpSpecialcase(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        List<Leave_paper> leave_papers = new ArrayList<>();
        List<LeavePaperDateToStringVo> leavePaperDateToStringVos =new ArrayList<>();
        //用户相关信息
        view.addObject("page","specialcase");
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",httpSession.getAttribute("classnode"));
        view.addObject("classname",httpSession.getAttribute("classname"));
        view.addObject("attendance",attendance);
        view.addObject("user",user);

        leavePaperDateToStringVos= teacherService.jumpSpecialcase((String)httpSession.getAttribute("username"),
                (String)httpSession.getAttribute("classnode"));

        view.addObject("leave_papers_list",leavePaperDateToStringVos);
        view.addObject("usertype",httpSession.getAttribute("usertype"));
        //功能信息
        view.setViewName("/content");
        return view ;
    }

    //同意假条
    @RequestMapping(value = "/agreeStuLeavePaper")
    public String agreeStudentLeavePaper( int id) {
        teacherService.agreeStudentLeavePaper(id);
        return "redirect:/Teacher/login/TeacherExam/loginUser/jump/specialcase";
    }
}
